﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using MyLib.Data;

namespace MyLib.Web {
	/// <summary>
	/// 
	/// </summary>
	[System.ComponentModel.DesignerCategory("Code")]
	public class MyWebService : WebService {
		private const string CONST_CODE_FILE_NAME = @"~/ConstSysCode.xml";

		/// <summary>
		/// 
		/// </summary>
		/// <param name="categoryId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		protected string GetCodeTextFromValue(int categoryId, object value) {
			return GetCodeTextFromValue(CONST_CODE_FILE_NAME, categoryId, value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="codeFileName"></param>
		/// <param name="categoryId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		protected string GetCodeTextFromValue(string codeFileName, int categoryId, object value) {
			System.Diagnostics.Debug.Assert(codeFileName != null);

			codeFileName = codeFileName.Trim();

			if (!codeFileName.StartsWith("~/") && !System.IO.Path.IsPathRooted(codeFileName)) {
				codeFileName = "~/" + codeFileName;
			}

			var cp = new CodeProvider(Context.Request.MapPath(codeFileName), new WebCacheProvider(Context));
			return cp.GetTextFromValue(categoryId, value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="categoryId"></param>
		/// <returns></returns>
		protected IEnumerable<Tuple<string, string, string>> GetCodesFromCategory(int categoryId) {
			return GetCodesFromCategory(CONST_CODE_FILE_NAME, categoryId);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="codeFileName"></param>
		/// <param name="categoryId"></param>
		/// <returns></returns>
		protected IEnumerable<Tuple<string, string, string>> GetCodesFromCategory(string codeFileName, int categoryId) {
			System.Diagnostics.Debug.Assert(codeFileName != null);

			codeFileName = codeFileName.Trim();

			if (!codeFileName.StartsWith("~/") && !System.IO.Path.IsPathRooted(codeFileName)) {
				codeFileName = "~/" + codeFileName;
			}

			var cp = new CodeProvider(Context.Request.MapPath(codeFileName), new WebCacheProvider(Context));

			return cp.GetCodesFromCategory(categoryId);
		}
	} // end of MyWebService.
}
